package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.FireZoneBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.RadioGroupUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/29.
 */

public class FireZoneReadActivity extends ActivityBase {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.ivHint)
    ImageView ivHint;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    @BindView(R.id.rbDensePlacesYes)
    RadioButton rbDensePlacesYes;
    @BindView(R.id.rbDensePlacesNo)
    RadioButton rbDensePlacesNo;
    @BindView(R.id.rgDensePlacesIsOK)
    RadioGroup rgDensePlacesIsOK;
    @BindView(R.id.tvDensePlacesProblem)
    TextView tvDensePlacesProblem;
    @BindView(R.id.tvDensePlacesMeasure)
    TextView tvDensePlacesMeasure;
    @BindView(R.id.rbFireDistrictYes)
    RadioButton rbFireDistrictYes;
    @BindView(R.id.rbFireDistrictNo)
    RadioButton rbFireDistrictNo;
    @BindView(R.id.rgFireDistrictHasUse)
    RadioGroup rgFireDistrictHasUse;
    @BindView(R.id.tvFireDistrictProblem)
    TextView tvFireDistrictProblem;
    @BindView(R.id.tvFireDistrictMeasure)
    TextView tvFireDistrictMeasure;
    @BindView(R.id.rbFireMeasuresYes)
    RadioButton rbFireMeasuresYes;
    @BindView(R.id.rbFireMeasuresNo)
    RadioButton rbFireMeasuresNo;
    @BindView(R.id.rgFireMeasuresIsOk)
    RadioGroup rgFireMeasuresIsOk;
    @BindView(R.id.tvFireMeasuresProblem)
    TextView tvFireMeasuresProblem;
    @BindView(R.id.tvFireMeasuresMeasure)
    TextView tvFireMeasuresMeasure;
    @BindView(R.id.tvGeneratorLocation)
    TextView tvGeneratorLocation;
    @BindView(R.id.tvOilStorage)
    TextView tvOilStorage;

    String build_id;

    @BindView(R.id.gvDensePlaces)
    NoScrollGridView gvDensePlaces;
    @BindView(R.id.gvFireDistrict)
    NoScrollGridView gvFireDistrict;
    @BindView(R.id.gvFireMeasures)
    NoScrollGridView gvFireMeasures;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_zone_read);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        getIntentData();
        setTop();
        setViewStatus();

        if (!TextUtils.isEmpty(build_id)) {
            //修改时，获取数据
            getData();
        }
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("防火分区与平面布置");
        tvSearch.setVisibility(View.GONE);
    }

    private void setViewStatus() {
        RadioGroupUtil.disableRadioGroup(rgDensePlacesIsOK);
        RadioGroupUtil.disableRadioGroup(rgFireDistrictHasUse);
        RadioGroupUtil.disableRadioGroup(rgFireMeasuresIsOk);
    }

    public void getData() {
        HttpMethods.getInstance().getFireZoneInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<FireZoneBean>(this) {
                    @Override
                    public void onNext(FireZoneBean bean) {
                        if (bean == null || "".equals(bean.getGson_null())) {
                            bean = new FireZoneBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            bean.setComply_safety_require(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            bean.setIs_occupy(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            bean.setIs_comply_safety_require(bc3);
                            bean.setGenerator_area("");
                            bean.setOil_storage_num("");
                        }

                        fillData(bean);
                    }
                });
    }

    private void fillData(FireZoneBean fireZoneBean) {
        if (fireZoneBean.getComply_safety_require().getIs_ok() == 1) {
            rbDensePlacesYes.setChecked(true);
        }

        tvDensePlacesProblem.setText(fireZoneBean.getComply_safety_require().getProblem());
        tvDensePlacesMeasure.setText(fireZoneBean.getComply_safety_require().getMeasures());

        if (fireZoneBean.getIs_occupy().getIs_ok() == 1) {
            rbFireDistrictYes.setChecked(true);
        }

        tvFireDistrictProblem.setText(fireZoneBean.getIs_occupy().getProblem());
        tvFireDistrictMeasure.setText(fireZoneBean.getIs_occupy().getMeasures());

        if (fireZoneBean.getIs_comply_safety_require().getIs_ok() == 1) {
            rbFireMeasuresYes.setChecked(true);
        } else if (fireZoneBean.getIs_comply_safety_require().getIs_ok() == 0) {
            rbFireMeasuresNo.setChecked(true);
        }

        tvFireMeasuresProblem.setText(fireZoneBean.getIs_comply_safety_require().getProblem());
        tvFireMeasuresMeasure.setText(fireZoneBean.getIs_comply_safety_require().getMeasures());

        tvGeneratorLocation.setText(fireZoneBean.getGenerator_area());
        tvOilStorage.setText(fireZoneBean.getOil_storage_num());

        initGridViewList(fireZoneBean);

    }

    void initGridViewList(FireZoneBean fireZoneBean) {
        if (fireZoneBean.getComply_safety_require().getImage() == null || fireZoneBean.getComply_safety_require().getImage().size() <= 0) {
            gvDensePlaces.setVisibility(View.GONE);
        } else {
            setGridviewAdapter(gvDensePlaces, fireZoneBean.getComply_safety_require().getImage());
        }


        if (fireZoneBean.getIs_occupy().getImage() == null || fireZoneBean.getIs_occupy().getImage().size() <= 0) {
            gvFireDistrict.setVisibility(View.GONE);
        } else {
            setGridviewAdapter(gvFireDistrict, fireZoneBean.getIs_occupy().getImage());
        }

        if (fireZoneBean.getIs_comply_safety_require().getImage() == null || fireZoneBean.getIs_comply_safety_require().getImage().size() <= 0) {
            gvFireMeasures.setVisibility(View.GONE);
        } else {
            setGridviewAdapter(gvFireMeasures, fireZoneBean.getIs_comply_safety_require().getImage());
        }
    }

    void setGridviewAdapter(GridView gridView, List<String> picList) {
        gridView.setVisibility(View.VISIBLE);
        //图片列表
        if (picList != null) {
            final String[] urls = new String[picList.size()];
            for (int i = 0; i < picList.size(); i++) {
                urls[i] = picList.get(i);
            }

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(view.getContext(), ImagePagerActivity.class);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    view.getContext().startActivity(intent);
                }
            });
            PicsAdapter picsAdapter = new PicsAdapter(gridView.getContext(), picList);
            gridView.setAdapter(picsAdapter);
        }
    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }
}
